# coding:utf-8
from snownlp import SnowNLP

from config import basepath
from sentiment.senti_dict.dictSentiment import DictSentiment


class DictEstimate(object):
    def __init__(self):
        self.TP = 0  # 预测为pos,预测对了
        self.FN = 0  # 预测为neg,预测错了
        self.FP = 0  # 预测为pos,预测错了
        self.TN = 0  # 预测为neg,预测对了
        # 加载数据
        self.pos_path = basepath + '/sentiment/estimate/data/test/pos.txt'
        self.neg_path = basepath + '/sentiment/estimate/data/test/neg.txt'
        self.testData = []
        # 积极
        with open(self.pos_path,'r', encoding='utf-8') as file:
            self.pos_list = [line[2:] for line in file.readlines()]
        # 消极
        with open(self.neg_path,'r', encoding='utf-8') as file:
            self.neg_list = [line[2:] for line in file.readlines()]
        self.length = len(self.pos_list)+len(self.neg_list)
        print(self.length)
        self.correct_num = 0

    def estimate(self):
        for sentence in self.pos_list:  # 预测积极测试集
            pos, neg = DictSentiment.sentiment(sentence)
            if pos>neg:
                # 预测为积极，对了
                self.TP += 1
                self.correct_num +=1
                # print(sentence,'1')
            else:
                # 预测为消极，错了
                self.FN += 1
                # print(sentence,'-1')
        for sentence in self.neg_list:
            pos, neg = DictSentiment.sentiment(sentence)
            if pos<neg:
                # 预测为消极，对了
                self.TN += 1
                self.correct_num += 1
                # print(sentence,'-1')
            else:
                self.FP +=1
                # print(sentence,'1')
    def recall(self):
        return self.TP/(self.TP+self.FN)


e = DictEstimate()
# # 评估
e.estimate()
print('pos共有%d个，预测正确%d个'%(2999, e.TP))
print('pos回召率:%f'%(e.TP/(e.TP+e.FN)))
print('neg共有%d个，预测正确%d个'%(2999, e.TN))
print('neg回召率:%f'%(e.TN/(e.TN+e.FP)))

# 总测试个数 5998
# 正确的个数 4959

# 其他数据集测试
# s = SnowNLP('我的这个朋友（小董的老板）查出了他贪污货款问题，这是民营企业最恼火的事儿，朋友怒气冲冲地要把小董送进号里，到处托朋友要重办他')
# print(s.sentiments)